﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Paneles;
using System.Windows.Forms;
using fideeraSoft.Entities;
using fideeraSoft.Utils;
using System.Data;
using System.Drawing;

namespace fideeraSoft.PanelesNuevosForms
{
    public class PanelAddProductoAProduccion:PanelGeneral
    {
        private TableLayoutPanel tlpBotones;
        private DataGridView dgvProducto;
        private Button btBuscarProdId;
        private TextBox tbBuscarProdCodigo;
        private TableLayoutPanel tlpBuscarProdId;
        private Button btVolver;
        private Button btGuardarCambios;
        private TableLayoutPanel tlpBuscarCodigo;
        private TextBox tbBuscarProdId;
        private Button btBuscarProdCodigo;
        private Form formR;
        private Producto product;
        private Label lbProduc;

        private System.Data.SqlClient.SqlDataAdapter adapter;
        private DataSet ds;

        private int locationTablaBuscarCodigo=0;
        private int locationTablaBuscarNombre = 0;

        public PanelAddProductoAProduccion(Form form,Label lbProducto,Producto producto)
        {
            this.formR = form;
            lbProduc= lbProducto;
            product = producto;

            InitializeComponent();

            this.formatearPanelSegunForm(formR);
            this.formatearTableTitulo("Agregar Producto a Producción");

            string query = "SELECT  ID AS idProducto,nombre AS Nombre, codigo AS Código , descripcion AS Descripción, color AS Color, marca AS Marca FROM Producto";
            adapter = SQLclass.obtenerAdapter(query);
            if (adapter != null)
            {

                ds = new DataSet();
                adapter.Fill(ds);

                dgvProducto.DataSource = ds.Tables[0];


                this.formatearDataGrid();
                this.formatearTablaBuscarProdNombre();
                this.formatearTablaBuscarProdCodigo();
                this.formatearTablaBotones();

                this.Controls.Add(dgvProducto);
                this.Controls.Add(tlpBuscarProdId);
                this.Controls.Add(tlpBuscarCodigo);

                this.Controls.Add(tlpBotones);
            }
            else
            {
                MessageBox.Show("NO HAY NINGUN PRODUCTO CARGADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.formR.Close();
            }


        }


        #region FORMATEAR

        private void formatearDataGrid()
        {
            dgvProducto.AutoGenerateColumns = true;
            dgvProducto.Size = new Size(this.Size.Width - 100, this.Size.Height - 400);
            dgvProducto.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvProducto.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvProducto.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvProducto.BorderStyle = BorderStyle.Fixed3D;

            dgvProducto.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvProducto.Size.Width), this.InitialRealPointVertical);
        }

        private void formatearTablaBuscarProdNombre()
        {
            tlpBuscarProdId.Controls.Add(btBuscarProdId);
            tlpBuscarProdId.Controls.Add(tbBuscarProdId);


            tlpBuscarProdId.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpBuscarProdId.BackColor = Color.LightCyan;
            tlpBuscarProdId.Size = new Size(400, tbBuscarProdId.Size.Height + 9);
            //tlpBuscarProv.Size = new Size(lbBuscarProvCuil.Size.Width + tbBuscarProvCuil.Size.Width + 10,tbBuscarProvCuil.Size.Height);
            int wid = MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBuscarProdId.Size.Width);

            locationTablaBuscarNombre= this.InitialRealPointVertical + dgvProducto.Size.Height + 15;

            tlpBuscarProdId.Location = new Point(wid, locationTablaBuscarNombre);


        }

        private void formatearTablaBuscarProdCodigo()
        {
            tlpBuscarCodigo.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpBuscarCodigo.BackColor = Color.LightCyan;
            tlpBuscarCodigo.Size = new Size(400, tbBuscarProdCodigo.Size.Height + 9);

            locationTablaBuscarCodigo = locationTablaBuscarNombre + tlpBuscarCodigo.Size.Height;

            tlpBuscarCodigo.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBuscarCodigo.Size.Width), locationTablaBuscarCodigo);

            tlpBuscarCodigo.Controls.Add(btBuscarProdCodigo);
            tlpBuscarCodigo.Controls.Add(tbBuscarProdCodigo);


        }

        private void formatearTablaBotones()
        {

            tlpBotones.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

            int locationBtCambios = locationTablaBuscarCodigo + tlpBuscarCodigo.Size.Height + 10;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBotones.Size.Width), locationBtCambios);

            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btGuardarCambios);

        }

        #endregion

        #region INICIALIZAR COMPONENTES
        private void InitializeComponent()
        {
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.dgvProducto = new System.Windows.Forms.DataGridView();
            this.btBuscarProdId = new System.Windows.Forms.Button();
            this.tbBuscarProdCodigo = new System.Windows.Forms.TextBox();
            this.tlpBuscarProdId = new System.Windows.Forms.TableLayoutPanel();
            this.btVolver = new System.Windows.Forms.Button();
            this.btGuardarCambios = new System.Windows.Forms.Button();
            this.tlpBuscarCodigo = new System.Windows.Forms.TableLayoutPanel();
            this.tbBuscarProdId = new System.Windows.Forms.TextBox();
            this.btBuscarProdCodigo = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dgvProducto)).BeginInit();
            this.SuspendLayout();
            // 
            // tlpBotones
            // 
            this.tlpBotones.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // dgvProducto
            // 
            this.dgvProducto.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvProducto.Location = new System.Drawing.Point(0, 0);
            this.dgvProducto.Name = "dgvProducto";
            this.dgvProducto.ReadOnly = true;
            this.dgvProducto.Size = new System.Drawing.Size(240, 150);
            this.dgvProducto.TabIndex = 0;
            this.dgvProducto.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvProducto_CellClick);
            // 
            // btBuscarProdId
            // 
            this.btBuscarProdId.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btBuscarProdId.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarProdId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarProdId.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarProdId.Location = new System.Drawing.Point(0, 0);
            this.btBuscarProdId.Name = "btBuscarProdId";
            this.btBuscarProdId.Size = new System.Drawing.Size(75, 23);
            this.btBuscarProdId.TabIndex = 0;
            this.btBuscarProdId.Text = "Buscar Producto (ID)";
            this.btBuscarProdId.UseVisualStyleBackColor = false;
            this.btBuscarProdId.Click += new System.EventHandler(this.btBuscarProdId_Click);
            // 
            // tbBuscarProdCodigo
            // 
            this.tbBuscarProdCodigo.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarProdCodigo.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarProdCodigo.MaxLength = 11;
            this.tbBuscarProdCodigo.Name = "tbBuscarProdCodigo";
            this.tbBuscarProdCodigo.Size = new System.Drawing.Size(140, 30);
            this.tbBuscarProdCodigo.TabIndex = 0;
            // 
            // tlpBuscarProdId
            // 
            this.tlpBuscarProdId.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBuscarProdId.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarProdId.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarProdId.ColumnCount = 2;
            this.tlpBuscarProdId.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProdId.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProdId.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarProdId.Name = "tlpBuscarProdId";
            this.tlpBuscarProdId.RowCount = 1;
            this.tlpBuscarProdId.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProdId.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProdId.Size = new System.Drawing.Size(400, 50);
            this.tlpBuscarProdId.TabIndex = 0;
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // btGuardarCambios
            // 
            this.btGuardarCambios.BackColor = System.Drawing.Color.AliceBlue;
            this.btGuardarCambios.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btGuardarCambios.Location = new System.Drawing.Point(0, 0);
            this.btGuardarCambios.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btGuardarCambios.Name = "btGuardarCambios";
            this.btGuardarCambios.Size = new System.Drawing.Size(75, 40);
            this.btGuardarCambios.TabIndex = 0;
            this.btGuardarCambios.Text = "Guardar";
            this.btGuardarCambios.UseVisualStyleBackColor = false;
            this.btGuardarCambios.Click += new System.EventHandler(this.btGuardarCambios_Click);
            // 
            // tlpBuscarCodigo
            // 
            this.tlpBuscarCodigo.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarCodigo.ColumnCount = 2;
            this.tlpBuscarCodigo.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarCodigo.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarCodigo.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarCodigo.Name = "tlpBuscarCodigo";
            this.tlpBuscarCodigo.RowCount = 1;
            this.tlpBuscarCodigo.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarCodigo.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarCodigo.Size = new System.Drawing.Size(400, 50);
            this.tlpBuscarCodigo.TabIndex = 0;
            // 
            // tbBuscarProdId
            // 
            this.tbBuscarProdId.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.tbBuscarProdId.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarProdId.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarProdId.MaxLength = 10;
            this.tbBuscarProdId.Name = "tbBuscarProdId";
            this.tbBuscarProdId.Size = new System.Drawing.Size(200, 30);
            this.tbBuscarProdId.TabIndex = 0;
            // 
            // btBuscarProdCodigo
            // 
            this.btBuscarProdCodigo.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btBuscarProdCodigo.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarProdCodigo.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarProdCodigo.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarProdCodigo.Location = new System.Drawing.Point(0, 0);
            this.btBuscarProdCodigo.Name = "btBuscarProdCodigo";
            this.btBuscarProdCodigo.Size = new System.Drawing.Size(75, 23);
            this.btBuscarProdCodigo.TabIndex = 0;
            this.btBuscarProdCodigo.Text = "Buscar Producto (Código)";
            this.btBuscarProdCodigo.UseVisualStyleBackColor = false;
            this.btBuscarProdCodigo.Click += new System.EventHandler(this.btBuscarProdCodigo_Click);
            ((System.ComponentModel.ISupportInitialize)(this.dgvProducto)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        private void btBuscarProdId_Click(object sender, EventArgs e)
        {
            if (dgvProducto.CurrentCell == null)
            {
                MessageBox.Show("NO HAY DATOS EN LA TABLA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (!string.IsNullOrWhiteSpace(tbBuscarProdId.Text))
            {
                string searchValue = tbBuscarProdId.Text;


                try
                {
                    foreach (DataGridViewRow row in dgvProducto.Rows)
                    {
                        if (row.Cells[0].Value.ToString().Equals(searchValue))
                        {
                            dgvProducto.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;

                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvProducto.CurrentCell = row.Cells[0];
                            tbBuscarProdId.Clear();
                            break;

                        }

                    }
                }
                catch (Exception exc)
                {
                    tbBuscarProdId.Clear();
                    MessageBox.Show("NO SE ENCONTRO EL PRODUCTO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO NOMBRE DE PRODUCTO A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void btBuscarProdCodigo_Click(object sender, EventArgs e)
        {
            if (dgvProducto.CurrentCell == null)
            {
                MessageBox.Show("NO HAY DATOS EN LA TABLA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (!string.IsNullOrWhiteSpace(tbBuscarProdCodigo.Text))
            {
                string searchValue = tbBuscarProdCodigo.Text;


                try
                {
                    foreach (DataGridViewRow row in dgvProducto.Rows)
                    {
                        if (row.Cells[2].Value.ToString().Equals(searchValue))
                        {
                            dgvProducto.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;

                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvProducto.CurrentCell = row.Cells[0];
                            tbBuscarProdCodigo.Clear();
                            break;

                        }

                    }
                }
                catch (Exception exc)
                {
                    tbBuscarProdCodigo.Clear();
                    MessageBox.Show("NO SE ENCONTRO EL PRODUCTO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO CÓDIGO DE PRODUCTO A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void btGuardarCambios_Click(object sender, EventArgs e)
        {
            if (dgvProducto.CurrentCell == null)
            {
                MessageBox.Show("NO HAY DATOS EN LA TABLA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            try
            {
                int rowIndex = dgvProducto.CurrentCell.RowIndex;

                product.IdProducto = Convert.ToInt32(dgvProducto.Rows[rowIndex].Cells[0].Value.ToString());
                product.Nombre = dgvProducto.Rows[rowIndex].Cells[1].Value.ToString();

                if (dgvProducto.Rows[rowIndex].Cells[1].Value != null) product.Marca = dgvProducto.Rows[rowIndex].Cells[1].Value.ToString(); else product.Marca = "";

                //product.Codigo =dgvProducto.Rows[rowIndex].Cells[2].Value.ToString();

                lbProduc.Text = product.Nombre + " ("+product.Marca+ ")";


                formR.Close();
            }
            catch (Exception ex)
            {
                CLogger.Append("[btGuardarCambios_Click] Exception: " + ex, CLogger.ERROR);      //DEBUG
            }
        }

        private void btVolver_Click(object sender, EventArgs e)
        {
            this.formR.Close();
        }

        private void dgvProducto_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvProducto.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

    }
}
